library NeutralVictimSpawnControl initializer init requires LegacySystem

	globals
		private trigger T
	endglobals

	private function AIPink takes nothing returns nothing
		local group g = CreateGroup()
		local group g2
		local unit u
		local real x
		local real y
		local integer i=1
		call GroupEnumUnitsOfPlayer(g,Player(13),null)
		if GetWidgetLife(udg_AITarget)<0.405 then
			set g2 = CreateGroup()
			loop
				exitwhen i>udg_Players
				if IsUnitVisible(udg_Hero[udg_Sorted[i]],Player(13)) then
					call GroupAddUnit(g2,udg_Hero[udg_Sorted[i]])
				endif
				set i=i+1
			endloop
			set u = GroupPickRandomUnit(g2)
			if u==null then
				set u = udg_Hero[udg_Sorted[GetRandomInt(1,udg_Players)]]
				// MZB Agro (useless)
				//if u==udg_Marksman and GetUnitAbilityLevel(udg_Marksman,'A016')!=0 and udg_Players>1 then
				//    set u = udg_Hero[udg_Sorted[GetRandomInt(1,udg_Players)]]
				//endif
			endif
			if GetWidgetLife(u)<=0.405 then
				set x = GetRandomReal(-7000.0,11000.0)
				set y = GetRandomReal(-4000.0,14000.0)
			else
				set x = GetUnitX(u)
				set y = GetUnitY(u)
			endif
			call DestroyGroup(g2)
			set g2 = null
		endif
		loop
			set u = FirstOfGroup(g)
			exitwhen u==null
			if GetWidgetLife(udg_AITarget)>0.405 then
				set x = GetUnitX(udg_AITarget)+GetRandomReal(-500.0,500.0)
				set y = GetUnitY(udg_AITarget)+GetRandomReal(-500.0,500.0)
			endif
			call IssuePointOrder(u,"attack",x,y)
			call GroupRemoveUnit(g,u)
		endloop
		call DestroyGroup(g)
		set g = null
		if GetRandomInt(0,12)==1 and udg_CurrentWave>2 then
			call StartSound(udg_Ambience[5]) //Rain Ambience
		elseif GetRandomInt(1,75)==1 then
			call StartSound(udg_Ambience[GetRandomInt(1,8)])
		elseif GetRandomInt(0,5)==1 then
			//call IssuePointOrder(udg_EvilSheep,"move",x,y)
		endif
	endfunction

	function SetAINeutralVictimEnabled takes boolean b returns nothing
		if b then
			call EnableTrigger(T)
		else
			call DisableTrigger(T)
		endif
	endfunction
	
	
	function TickAINeutralVictim takes nothing returns nothing
		call TriggerExecute(T)
	endfunction
	
	private function init takes nothing returns nothing
		set T = CreateTrigger()
        call TriggerRegisterTimerEvent(T, 6.1, true)
		call DisableTrigger(T)
		call TriggerAddAction(T,function AIPink)
	endfunction

endlibrary